// *** LICENSE HEADER ***
// Filename: ./node.h
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#ifndef NODEWARS_NODE_H
#define NODEWARS_NODE_H

#include <SDL.h>
#include <GL/gl.h>
#include <cmath>
#include <cfloat>
#include "nodegraph.h"
#include "connnode.h"
#include "nodegraph.h"
#include "light.h"
#include "helperfunctions.h"

class connNode;
class nodeGraph;

class resourceAttack;
class resourceDefence;
class resourceTakeover;
class resourceLives;
class resourceCredits;

//Class that handles the node (as in the game) data and contains some node-relevant methods
class node {
    public:
        friend class nodeGraph;
        friend class resourceAttack;
        friend class resourceDefence;
        friend class resourceTakeover;
        friend class resourceLives;
        friend class resourceCredits;
        node(node* newNext, nodeGraph* newPar, float newX, float newY);
        ~node();

        //Copy atributes from another node
        void CopyAtributesFrom(node* other);

        //Connect this node to another node and make the connection visible, if needed
        void Connect(node* B, bool visible = false);
        //Check, whether this node is connected to another node
        bool IsConnectedTo(node* B);
        //Disconnect this node from the other node
        void Disconnect(node* B);
        //Disconnect this node from all the nodes
        void DisconnectAll();
        //Check, whether the node belongs to a same node graph as the other node
        bool IsFriendly(node* other);
        //Increase the node's health
        void Grow();

        //Damage the node
        bool Damage(int damageDealt);
        //Improve the node's attack
        void ImproveAttack();
        //Defend the node
        void Defend(int healthReceived);
        //Improve the node's defence
        void ImproveDefence();
        //Perform a friendly takeover on the node
        void FriendlyTakeover(int psiReceived);
        //Perform an enemy takeover on the node
        bool EnemyTakeover(int psiReceived);
        //Improve the node's takeover
        void ImproveTakeover();

        //Some probability calculation functions
        float CalculateProbabilityOfAttack(node* other);
        float CalculateProbabilityOfAttack(float pX, float pY);
        float CalculateProbabilityOfDefence(node* other);
        float CalculateProbabilityOfDefence(float pX, float pY);
        float CalculateProbabilityOfTakeover(node* other);
        float CalculateProbabilityOfTakeover(float pX, float pY);

        //Distance calculation functions
        float CalculateDistanceTo(float pX, float pY);
        float CalculateDistanceTo(node* another);

        //Reset the node light
        void ResetLight();

        //Draw the node (and show the energies)
        void Draw(bool showEnergies);
        //Draw the node's boundary (friendly or foe)
        void DrawBoundary(bool friendly);

        //Get the node data
        float GetX();
        float GetY();
        int GetThetaEnergy();
        int GetAttackEnergy();
        int GetDefenceEnergy();
        int GetTakeoverEnergy();

        //Set the node data
        void SetTakeoverEnergy(int newTakeover);

        //(De)select the node
        void Deselect();
        void Select();
    private:
        //Next node in node graph linked list
        node* next;
        //Connetions to other nodes
        connNode* connections;
        //Parent of the node
        nodeGraph* parent;

        //Light of the node
        light* myLight;

        //Node data
        float x;
        float y;
        int thetaEnergy;
        int attack;
        int defence;
        int takeover;

        bool selected;

        //Node growth helper data
        int growthAmmount[5];
        int growthWave[5];
};

#endif
